package com.hank.controller;

import com.hank.dto.UserDTO;
import com.hank.util.JwtUtils;
import com.hank.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.hank.filter.UserContextFilter.getCurrentUser;

/**
 * @author gaoZhiHao
 * @date 2023/5/11 15:36
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private JwtUtils jwtUtils;

    @PostMapping("/token")
    public Result generateToken(@RequestBody UserDTO userDTO) {
        // 生成JWT token
        return Result.ok(jwtUtils.generateToken(userDTO));
    }

    @GetMapping("/businessUserInfo")
    public String doBusinessUserInfo() {
        // 获取当前线程中的用户信息
        UserDTO currentUser = getCurrentUser();
        if (currentUser != null) {
            return "当前用户：" + currentUser.getName();
        } else {
            return "尚未获取到用户信息";
        }
    }
}
